﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Quartz;
using TjZheng.Framework;
using CJAtech.BBS.Business;
using CJAtech.BBS.DataEntity;
using TjZheng.Base;
using System.Threading;
using TjZheng.Logging;

namespace TjZheng.SyncBBSRedis
{
    public class SysArticleMove : TjZhengJob
    {
        public string BBSName
        {
            get
            {
                return ConfigHelper.GetValue("BBSName");
            }
        }
        public int FromCategoryId
        {
            get
            {
                return ConfigHelper.GetIntValue("FromCategoryId", 1);
            }
        }
        public int ToCategoryId
        {
            get
            {
                return ConfigHelper.GetIntValue("ToCategoryId", 2);
            }
        }
        public override void JobExecute(global::Quartz.IJobExecutionContext context)
        {
            //注册Sql日志
            ContextContainer.RegisterWriteSqlLog((sender, e) =>
            {
                LogHelper.WriteSqlLog(e.DbHelper.ConnectionStringKey, e.SqlString, e.DbParameters);
            });

            BizCommunityConfig objBizCommunityConfig = new BizCommunityConfig();
            CommunityConfig objCommunityConfig = objBizCommunityConfig.GetRecord("ConfigCode=@ConfigCode", "ConfigCode".CreateSqlParameters(this.BBSName));
            if (objCommunityConfig == null)
            {
                ConsoleHelper.WriteLineYellow("论坛数据配置不能为空");
                return;
            }
            BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo(objCommunityConfig.ConnectionStringKey, objCommunityConfig.DataObjectParam);
            var objFromCategory = objBizCategoryInfo.GetRecord(FromCategoryId);
            if (objFromCategory == null)
            {
                ConsoleHelper.WriteLineYellow("源版块不能为空");
                return;
            }
            var objToCategory = objBizCategoryInfo.GetRecord(ToCategoryId);
            if (objToCategory == null)
            {
                ConsoleHelper.WriteLineYellow("去版块不能为空");
                return;
            }
            BizArticleInfo objBizArticleInfo = new BizArticleInfo(objCommunityConfig.ConnectionStringKey, objCommunityConfig.DataObjectParam);
            List<ArticleInfo> objList = null;
            int pageIndex = 0;
            ConsoleHelper.WriteLineYellow(string.Format("开始同步：源版块【{0}】，去版块【{1}】", FromCategoryId, ToCategoryId));
            while (true)
            {
                objList = objBizArticleInfo.GetList("CategoryId=" + FromCategoryId, "", "top 10 *");
                if (objList.IsNull())
                {
                    ConsoleHelper.WriteLineYellow(string.Format("第{0}页没有数据，同步结束", pageIndex + 1));
                    break;
                }
                foreach (ArticleInfo objArticleInfo in objList)
                {
                    objArticleInfo.CategoryId = objToCategory.CategoryId;
                    objArticleInfo.CategoryIDPath = objToCategory.IDPath;
                    objArticleInfo.SetState(EntityStatus.Modified);
                }
                objBizArticleInfo.Batch(objList);
                Thread.Sleep(10);
                ConsoleHelper.WriteLineYellow(string.Format("第{0}页数据已同步", pageIndex + 1));
                pageIndex++;
            }
        }
    }
}
